﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


using KinnectInput.KinectIcons;
//Tengo que usar un alias porque sino colisiona
using jupLib=JupiterLibrary;

namespace KinnectInput.KinectVideo
{
    public enum KinectVideoPlayerState
    {
        Colasped,
        Visible,
    }
    
    public class KinectVideoPlayer
    {

        public Microsoft.Xna.Framework.Media.VideoPlayer myPlayer { get; private set; }
        public Microsoft.Xna.Framework.Media.Video movie { get; private set; }
        Rectangle imageSize;
        jupLib.Sprite NoVideo;
        public VideoPlayerControlBar ControlBar { get; private set; }
        jupLib.Sprite VideoDisplay;
        public KinectVideoPlayerState VideoPlayerState;
        //public KinectMovieState MovieState;
        public VideoSpeechControl speechControl { get; private set; }
        bool speechPlay, speechStop;

        public KinectVideoPlayer(Video movie, Rectangle imageSizeRect)
        {
            this.movie = movie;
            myPlayer = new VideoPlayer();
            this.imageSize = imageSizeRect;
            BuildPlayer(imageSizeRect);
            VideoPlayerState=KinectVideoPlayerState.Colasped;
            
        }

        void speechControl_StopTold(object sender, EventArgs e)
        {
            speechStop = true;
        }

        void speechControl_PlayTold(object sender, EventArgs e)
        {
            speechPlay = true;
        }

        private void BuildPlayer(Rectangle imageSizeRect)
        {
            VideoDisplay = new jupLib.Sprite();
            VideoDisplay.SetRectangle(imageSizeRect);

            NoVideo = new jupLib.Sprite();
            NoVideo.SetRectangle(imageSizeRect);
            NoVideo.SetColor(new Color(0, 0, 0.25f,0.9f));
            NoVideo.Update();
           
            ControlBar = new VideoPlayerControlBar(imageSizeRect, false);
        }

        bool _disposingSpeech;

        public void Update()
        {

            if (myPlayer != null && movie != null && myPlayer.State == MediaState.Playing)
            {
                VideoDisplay.Texture = myPlayer.GetTexture();
            }

            if (this.VideoPlayerState == KinectVideoPlayerState.Colasped)
            {
                
                if (speechControl!=null && !_disposingSpeech)
                {
                    _disposingSpeech = true;

                    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                    {
                        try
                        {
                            if (speechControl != null)
                            {
                                speechControl.Stop();
                                speechControl.Dispose();
                                speechControl = null;
                            }
                        }
                        finally
                        {
                            _disposingSpeech = false;
                        }
                    });
                }
            }
            else
            {
                if (speechControl == null)
                {
                    speechControl = new VideoSpeechControl();
                    speechControl.PlayTold += new EventHandler(speechControl_PlayTold);
                    speechControl.StopTold += new EventHandler(speechControl_StopTold);
                    speechControl.Start();
                }
            }

            VideoDisplay.Update();
            ControlBar.Update();
        }

        public void Draw()
        {
            if (VideoPlayerState == KinectVideoPlayerState.Visible)
            {
                if (myPlayer.State == MediaState.Stopped)
                {
                    NoVideo.Draw();
                }
                else
                {
                    if (myPlayer.State == MediaState.Playing || myPlayer.State == MediaState.Paused)
                    {
                        VideoDisplay.Draw();
                    }
                    else
                        NoVideo.Draw();
                }

                ControlBar.Draw();
            }
        }

        public void CheckButtons(Vector2 v)
        {
            
            if (myPlayer.State!=MediaState.Playing && ControlBar.PlayBtn.ButtonRect.Contains((int)v.X, (int)v.Y))
            {
                //si boton selccionado pasas a seleccionado
                if (ControlBar.PlayBtn.PlayerButtonState == VideoPlayerButtonState.Selected)
                {
                    if (myPlayer.State == MediaState.Paused)
                    {
                        myPlayer.Resume();
                    }
                    else
                    {
                        myPlayer.Play(movie);
                    }
                }
                else
                {
                    ControlBar.PlayBtn.PlayerButtonState = VideoPlayerButtonState.Active;
                }
                ControlBar.PauseBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                ControlBar.StopBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                
            }
            else if ((myPlayer.State != MediaState.Stopped && ControlBar.StopBtn.ButtonRect.Contains((int)v.X, (int)v.Y)))
            {
                if (ControlBar.StopBtn.PlayerButtonState == VideoPlayerButtonState.Selected)
                {
                    myPlayer.Stop();
                }
                else
                {
                    ControlBar.StopBtn.PlayerButtonState = VideoPlayerButtonState.Active;
                }
                ControlBar.PauseBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                ControlBar.PlayBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
            }
            else if ((myPlayer.State != MediaState.Paused && ControlBar.PauseBtn.ButtonRect.Contains((int)v.X, (int)v.Y)))
            {
                if (ControlBar.PauseBtn.PlayerButtonState == VideoPlayerButtonState.Selected)
                {
                    myPlayer.Pause();
                }
                else
                {
                    ControlBar.PauseBtn.PlayerButtonState = VideoPlayerButtonState.Active;
                }
                ControlBar.StopBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                ControlBar.PlayBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
            }
            else
            {
                
                //ControlBar.PlayBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                //ControlBar.PauseBtn.PlayerButtonState = VideoPlayerButtonState.InActive;
                //ControlBar.StopBtn.PlayerButtonState = VideoPlayerButtonState.InActive;

                VideoPlayerButton[] VPBtns=new VideoPlayerButton[]{ControlBar.PlayBtn,ControlBar.StopBtn,ControlBar.PauseBtn};
                foreach (VideoPlayerButton VPB in VPBtns)
                {
                    VPB.PlayerButtonState = VideoPlayerButtonState.InActive;
                    VPB.SelectionButtonRing.StartAngle = 0f;
                    VPB.SelectionButtonRing.Update();
                }
            }

            if (speechPlay)
            {
                speechPlay = false;
                if (myPlayer.State == MediaState.Paused)
                {
                    myPlayer.Resume();
                }
                else
                {
                    myPlayer.Play(movie);
                }
            }

            if (speechStop)
            {
                speechStop = false;
                myPlayer.Stop();
            }
            
        }
        public void CheckButtons(Point p)
        {
            CheckButtons(new Vector2(p.X, p.Y));
        }
    }
}
